using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Commonplace.Domain;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Commonplace
{
	/// <summary>
	/// Summary description for DetailList.
	/// </summary>
	public class DetailList : System.Windows.Forms.UserControl
	{
		private Commonplace.VScrollPanel _displayPanel;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        List<DetailControl> _detailControls = new List<DetailControl>();
        List<DetailRecord> _details = new List<DetailRecord>();
        bool _layoutSuspended = false;

        public bool LayoutSuspended
        {
            get { return _layoutSuspended; }
            set 
            { 
                _layoutSuspended = value;

                if (true == value)
                {
                    SuspendLayout();
                    _displayPanel.SuspendLayout();
                }
                else
                {
                    _displayPanel.ResumeLayout();
                    ResumeLayout();

                    Rearrange();
                }
            }
        }

		public ReadOnlyCollection<DetailControl> DetailControls
		{
			get 
			{ 
                return _detailControls.AsReadOnly();
			}
		}

        public List<DetailRecord> Details
        {
            get { return _details; }
        }

		public DetailList()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

		}

        public void Remove(DetailRecord record)
        {
            foreach (DetailControl detail in _detailControls)
            {
                if (detail.Detail == record)
                {
                    Detail_Remove(detail, EventArgs.Empty);
                    break;
                }
            }
        }

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this._displayPanel = new Commonplace.VScrollPanel();
			this.SuspendLayout();
			// 
			// _displayPanel
			// 
			this._displayPanel.AutoScroll = true;
			this._displayPanel.Dock = System.Windows.Forms.DockStyle.Fill;
			this._displayPanel.Location = new System.Drawing.Point(0, 0);
			this._displayPanel.Name = "_displayPanel";
			this._displayPanel.Size = new System.Drawing.Size(536, 288);
			this._displayPanel.TabIndex = 0;
			// 
			// ImageDetailList
			// 
			this.Controls.Add(this._displayPanel);
			this.Name = "ImageDetailList";
			this.Size = new System.Drawing.Size(536, 288);
			this.ResumeLayout(false);

		}
		#endregion

		public void ClearFiles()
		{
			_displayPanel.Controls.Clear();

			_detailControls.Clear();
            _details.Clear();
		}			

		public void AddFile(DetailRecord detail)
		{
            _details.Add(detail);

			DetailControl d = detail.CreateDetailControl();
			d.Remove += new EventHandler(Detail_Remove);
			_detailControls.Add(d);
			
			AddDetail(d);
		}

		private void Detail_Remove(object sender, EventArgs e)
		{
			DetailControl detail = sender as DetailControl;
			if (null != detail)
			{
				_displayPanel.Controls.Remove(detail);
				_detailControls.Remove(detail);
                _details.Remove(detail.Detail);
				Rearrange();
			}
		}

		private void AddDetail(DetailControl d)
		{
			_displayPanel.Controls.Add(d);

			Size startSize = _displayPanel.ClientSize;

			Rearrange();

			if (false == _displayPanel.ClientSize.Equals(startSize))
				Rearrange();

		}

		private void Rearrange()
		{
            if (false == _layoutSuspended)
    			_displayPanel.SuspendLayout();

			for (int i = 0; i < _detailControls.Count; i++)
			{
				DetailControl tc = _detailControls[i] as DetailControl;

				tc.Top = (int) i * 180 + _displayPanel.AutoScrollPosition.Y;
				tc.Height = 160;
				tc.Left =  _displayPanel.AutoScrollPosition.X;
				tc.Width = _displayPanel.ClientSize.Width - 10;
				tc.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
			}

            if (false == _layoutSuspended)
                _displayPanel.ResumeLayout();

		}
	}
}
